package com.javasm.service.stockService.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javasm.controller.B2CController.OrderController;
import com.javasm.controller.goodsController.GoodsController;
import com.javasm.domin.entity.stockEntity.OutDetailedOrder;
import com.javasm.domin.entity.stockEntity.OutStorageImel;
import com.javasm.domin.vo.stockVo.OutDetailedOrderImelVo;
import com.javasm.domin.vo.stockVo.OutDetailedOrderTempVo;
import com.javasm.domin.vo.stockVo.OutDetailedOrderVo;
import com.javasm.mapper.stockMapper.OutBriefListMapper;
import com.javasm.mapper.stockMapper.OutDetailedOrderMapper;
import com.javasm.mapper.stockMapper.OutStorageImelMapper;
import com.javasm.service.stockService.OutDetailedOrderService;
import com.javasm.service.stockService.OutStorageImelService;
import com.javasm.transfer.stockTransfer.OutDetailedOrderTransfer;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【stock_sales_out_detailed_order(销售出库详细列表)】的数据库操作Service实现
 * @createDate 2021-11-19 10:33:30
 */

@Service
@Transactional
public class OutDetailedOrderServiceImpl extends ServiceImpl<OutDetailedOrderMapper, OutDetailedOrder> implements OutDetailedOrderService {
    @Resource
    private OutDetailedOrderMapper outDetailedOrderMapper;
    @Resource
    private OutDetailedOrderService outDetailedOrderService;
    @Resource
    private OutStorageImelService outStorageImelService;
    @Resource
    private OutDetailedOrderTransfer outDetailedOrderTransfer;

    @Resource
    private OrderController orderController;

    @Resource
    private GoodsController goodsController;

    @Resource
    private OutBriefListMapper outBriefListMapper;

    @Resource
    private OutStorageImelMapper outStorageImelMapper;


    //查询出库商品列表
    @Override
    public List<OutDetailedOrderVo> selectDetailedId(Serializable id) {
        List<OutDetailedOrder> orders = outDetailedOrderMapper.selectDetailedId(id);
        return outDetailedOrderTransfer.toVO(orders);
    }


    /**
     * 优质代码查询商品串号
     *
     * @param id
     * @return
     */
    @Override
    public List<OutDetailedOrderImelVo> selectDetailedImelId(Serializable id) {
        List<OutDetailedOrder> orders = outDetailedOrderMapper.selectDetailedId(id);
        HashMap<Integer, List<String>> map = new HashMap<>();
        for (OutDetailedOrder order : orders) {
            List<OutDetailedOrderTempVo> tempVos = outDetailedOrderMapper.selectDetailedIdImel(order.getImelId());
            ArrayList<String> imel = new ArrayList<>();
            for (OutDetailedOrderTempVo tempVo : tempVos) {
                imel.add(tempVo.getOutImel());
            }
            map.put(order.getId(), imel);
        }
//        System.out.println(map);
        //得到了个逗比

        List<OutDetailedOrderImelVo> outDetailedOrderImelVos = new ArrayList<>();
        for (OutDetailedOrder order : orders) {
            OutDetailedOrderImelVo imelVo = new OutDetailedOrderImelVo();
            BeanUtils.copyProperties(order, imelVo);
            imelVo.setOutImel(map.get(order.getId()));
            System.out.println(imelVo);
            outDetailedOrderImelVos.add(imelVo);
        }
        return outDetailedOrderImelVos;
    }

    @Override
    public void insertImel(List<String> ids) {
        //更新本地数据库订单状态
        //商品id 下标为0
        outBriefListMapper.updateOrderStatus(ids.get(0));
        //更新另一个订单表的库存
        orderController.update(ids.get(0));
        //减少库存
        List<OutDetailedOrder> allByOrderId = outDetailedOrderMapper.findAllByOrderId(Integer.valueOf(ids.get(0)));
        for (OutDetailedOrder outDetailedOrder : allByOrderId) {
            goodsController.updateByIdStock(outDetailedOrder.getOrderId().longValue(),outDetailedOrder.getGoodsNum().longValue());
        }
        List<OutDetailedOrder> detailedOrders = outDetailedOrderMapper.selectDetailedId(ids.get(0));
        ids.remove(0);
        for (OutDetailedOrder order : detailedOrders) {
            for (int i = 0; i < order.getGoodsNum(); i++) {
                OutStorageImel outStorageImel = new OutStorageImel();
                outStorageImel.setOutImel(ids.get(0));
                if (ids.size()!=0){
                    ids.remove(0);
                }
                outStorageImel.setImelId(order.getImelId());
                outStorageImelService.save(outStorageImel);
            }
        }

    }


    @Override
    public void holdImel(List<String> ids) {
        List<OutDetailedOrder> detailedOrders = outDetailedOrderMapper.selectDetailedId(ids.get(0));
        ids.remove(0);
        for (OutDetailedOrder order : detailedOrders) {
            for (int i = 0; i < order.getGoodsNum(); i++) {
                OutStorageImel outStorageImel = new OutStorageImel();
                outStorageImel.setOutImel(ids.get(0));
                if (ids.size() != 0) {
                    ids.remove(0);
                }
                outStorageImel.setImelId(order.getImelId());
                outStorageImelService.save(outStorageImel);
            }
        }
    }
}
